package cn.itsource.ymcc.service.impl;

import cn.itsource.ymcc.constant.VideoCons;
import cn.itsource.ymcc.domain.MediaFile;
import cn.itsource.ymcc.result.JsonResult;
import cn.itsource.ymcc.service.MediaProducer;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class MediaProducerImpl implements MediaProducer {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;


    @Override
    public JsonResult syncSend(MediaFile mediaFile) {
        //将对象转为json字符  消费那边要转回来
        String message = JSON.toJSONString(mediaFile);
        //发送异步消息    主题 tag
        SendResult sendResult = rocketMQTemplate.syncSend(VideoCons.TOPIC_COURSE_VIDEO_PUSH_TO_SRS + ":" +
                VideoCons.TAGS_COURSE_VIDEO_PUSH_TO_SRS, message);
        //得到发送消息的状态
        SendStatus sendStatus = sendResult.getSendStatus();
        //判断有没有发送成功
        return sendStatus == SendStatus.SEND_OK?JsonResult.success():JsonResult.error("消息发送失败");
    }
}
